package com.ymx.darling.mask;

import com.ymx.darling.type.TypeHandler;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 数据脱敏的类型处理器
 * 该处理器只会在结果集转实体类的过程中处理脱敏数据
 * @author 爱Java的小于
 */
public class MaskTypeHandler implements TypeHandler<String> {
    private final String maskType;

    public MaskTypeHandler(String maskType) {
        this.maskType = maskType;
    }

    @Override
    public String getResult(ResultSet resultSet, int columIndex) throws SQLException {
        return MaskManager.mask(this.maskType, resultSet.getString(columIndex));
    }

    /**
     * 此方法不会被调用
     * @param statement PreparedStatement
     * @param value 要插入的值
     * @param index 索引
     */
    @Override
    public void setParameter(PreparedStatement statement, String value, int index) throws SQLException {
        statement.setString(index, value);
    }
}
